package com.sangu.utils;

import java.util.List;
import java.util.stream.Collectors;

public class BeanUtils {

    //该类不能被new出来
    private BeanUtils() {
    }

    //拷贝对象，声明泛型方法，返回值类型
    public static <V> V copyBean(Object source,Class<V> clazz){
        V instance = null;
        try {
            //创建实例
            instance = clazz.newInstance();
            //实现属性复制
            org.springframework.beans.BeanUtils.copyProperties(source,instance);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //返回结果的
        return instance;
    }

    //V是返回值类型，O是入参类型
    public static <O,V> List<V> copyBeanList(List<O> list,Class<V> clazz){
        List<V> collect = list.stream().map(o -> copyBean(o, clazz)).collect(Collectors.toList());
        return collect;
    }


}
